package org.cn.cf.db;


import java.sql.Connection;
import java.sql.Savepoint;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class CoolfaceDB {
	private static final Log LOG = LogFactory.getLog(CoolfaceDB.class);
	private ConnectBean conBean;
	private Connection  con;
	
	private CoolfaceDB(){
		conBean = ConBeanFactory.getConBean();
		conBean.setBUse(true);
		conBean.setIStartUsing(System.currentTimeMillis());
		con = conBean.getCon();
	}
	public static CoolfaceDB instance(){
		return new CoolfaceDB();
	}
	
	public Connection getConnection(){
		if(con==null) 
			con = conBean.getCon();	
		return con;
	}
	
	public void setAutoCommit(boolean bAuto){
		try{
			con.setAutoCommit(bAuto);
		}catch(Exception e){

			LOG.error(e.getMessage());
		}
	}
	
	public Savepoint savePoint(){
		Savepoint point = null;
		try{
			point = con.setSavepoint();
		}catch(Exception e){
			point = null;
		}
		return point;
	}
	
	public void roolback(){		
		try{
	
			con.rollback();
		}catch(Exception e){
			//e.printStackTrace();
			LOG.error(e.getMessage());
		}
	}
	public void releaseSavepoint(Savepoint point){		
		try{
			con.releaseSavepoint(point);
		}catch(Exception e){
			
			LOG.error(e.getMessage());
		}
	}
	
	public void roolback(Savepoint point){
		try{
			con.rollback(point);
		}catch(Exception e){
			
			LOG.error(e.getMessage());
		}
	}
	
	public void commit(){
		try{
			con.commit();
		}catch(Exception e){
			
			LOG.error(e.getMessage());
		}
	}
	
	public void close(){
		conBean.setBUse(false);
		conBean.setIStartUsing(-1);
	}

}
